package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author SunSun
 * @date 2021/10/25
 */
@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /*
    发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {

        movementService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 我的动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findById(Long userId,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pagesize){

        PageResult pr=movementService.findById(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult pr=movementService.findFriendMovement(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity findRecommendMovements(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult pr=movementService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看单个动态
     * @param movementID
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findOne(@PathVariable("id") String movementID){
         MovementsVo vo=movementService.findOne(movementID);
         return ResponseEntity.ok(vo);
    }

    /**
     * 点赞
     * @param movementID
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementID){
        Integer likeCount=commentService.likecomment(movementID);

        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     * @param movementID
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementID){
        Integer likeCount=commentService.dislikecomment(movementID);

        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/love")
    public  ResponseEntity love(@PathVariable("id") String movementID){
        Integer loveCount=commentService.lovecomment(movementID);

        return ResponseEntity.ok(loveCount);
    }
    @GetMapping("/{id}/unlove")
    public ResponseEntity dislove(@PathVariable("id") String movementID){
        Integer disloveCount=commentService.dislovecomment(movementID);

        return ResponseEntity.ok(disloveCount);
    }

    /**
     * 谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo>list=movementService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }

}
